<!DOCTYPE html>

<html lang="en">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>数据库迁移（Database Migrations） &mdash; Phalcon 2.0.0 文档</title>
    <meta name="keywords" content="php, phalcon, phalcon php, php framework, faster php framework"><link rel="stylesheet" type="text/css" href="../_static/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="http://static.phalconphp.com/css/phalcon.min.css" />
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400' rel='stylesheet' type='text/css'>
    <link href="http://fonts.googleapis.com/css?family=Merriweather:400,700" rel="stylesheet" type="text/css" />
    <!--
    EUROPE <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=latin-ext' rel='stylesheet' type='text/css'>
    GREEK <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=greek-ext' rel='stylesheet' type='text/css'>
    RUSSIA <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=cyrillic-ext,latin' rel='stylesheet' type='text/css'>
    -->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.0.0',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script src="../_static/jquery.min.js"></script>
    <script type="text/javascript" src="../_static/docs.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="top" title="Phalcon 2.0.0 文档" href="../index.html" />
    <link rel="next" title="调试应用程序（Debugging Applications）" href="debug.html" />
    <link rel="prev" title="国际化（Internationalization）" href="intl.html" /> 
  </head>
  <body>

<header class="page-header">
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-container">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand phalcon-logo" href="/"><span itemprop="name" class="sr-only">Phalcon PHP</span></a>
            </div>

            <div class="collapse navbar-collapse navbar-right" id="main-menu-container">
                <ul class="nav navbar-nav main-menu">
                  <li class="first"><a href="http://phalconphp.com/en/download" class="header-nav-link">Download</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="http://blog.phalconphp.com/" class="header-nav-link" target="_blank">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                </ul>
            </div>
        </div>
    </nav>
  </header>

<div class="heading">
    <div class="container">
        <div class="row">
            <h2>Documentation</h2>
        </div>
    </div>
</div>
    <!--<div class="header-line">
      <div class="size-wrap">
        <div class="header-line-title title-white">Documentation</div>
      </div>
    </div>-->
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="debug.html" title="调试应用程序（Debugging Applications）"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="intl.html" title="国际化（Internationalization）"
             accesskey="P">上一页</a> |</li>
        <li><a href="http://phalconphp.com">Home</a> &raquo;</li>
        <li><a href="../index.html">Phalcon 2.0.0 文档</a> &raquo;</li> 
      </ul>
    </div>  

      <table width="100%" align="center" cellpadding="0" cellspacing="0">
        <tr>
      <td class="primary-box" width="25%" valign="top">
            <div>
            <div id="searchbox" style="">
                <!--<form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="search" name="q" size="25" placeholder="Search">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>-->
                <div style="width:200px;padding:10px">
                  <gcse:searchbox-only></gcse:searchbox-only>
                </div>
            </div>
            </div>
            <div style="padding:5px;padding-left:10px">
              <div id="carbonads-container">
                <div class="carbonad"><div id="azcarbon"></div>
                <script type="text/javascript">var z = document.createElement("script"); z.type = "text/javascript"; z.async = true; z.src = "http://engine.carbonads.com/z/56496/azcarbon_2_1_0_VERT"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s);</script>
                </div></div>
            </div>
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference internal" href="#">数据库迁移（Database Migrations）</a><ul>
<li><a class="reference internal" href="#schema-dumping">图解导出（Schema Dumping）</a></li>
<li><a class="reference internal" href="#migration-class-anatomy">迁移类剖析（Migration Class Anatomy）</a><ul>
<li><a class="reference internal" href="#defining-columns">定义列（Defining Columns）</a></li>
<li><a class="reference internal" href="#defining-indexes">定义索引（Defining Indexes）</a></li>
<li><a class="reference internal" href="#defining-references">定义关系（Defining References）</a></li>
</ul>
</li>
<li><a class="reference internal" href="#writing-migrations">创建迁移类（Writing Migrations）</a></li>
<li><a class="reference internal" href="#running-migrations">执行迁移（Running Migrations）</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="intl.html" title="上一章">&lt; 国际化（Internationalization）</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="debug.html" title="下一章">调试应用程序（Debugging Applications） &gt;</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/reference/migrations.txt" rel="nofollow">显示源代码</a></li>
            </ul>
        </td>
          <td class="second-box" valign="top">
            <div class="document">
                <div class="documentwrapper">
                  <div class="bodywrapper">
                    <div class="body" >
                      
  <div class="section" id="database-migrations">
<h1>数据库迁移（Database Migrations）<a class="headerlink" href="#database-migrations" title="永久链接至标题">¶</a></h1>
<p>Migrations are a convenient way for you to alter your database in a structured and organized manner.</p>
<blockquote class="highlights">
<div><strong>Important:</strong> Migrations are available on <a class="reference internal" href="tools.html"><em>Phalcon Developer Tools</em></a> You need at least Phalcon Framework version 0.5.0 to use developer tools. Also is recommended to have PHP 5.4 or greater installed.</div></blockquote>
<p>Often in development we need to update changes in production environments. Some of these changes could be database modifications like new fields, new tables, removing indexes, etc.</p>
<p>When a migration is generated a set of classes are created to describe how your database is structured at that moment. These classes can be used to synchronize the schema structure on remote databases setting your database ready to work with the new changes that your application implements. Migrations describe these transformations using plain PHP.</p>
<div align="center">
    <iframe src="http://player.vimeo.com/video/41381817" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div><div class="section" id="schema-dumping">
<h2>图解导出（Schema Dumping）<a class="headerlink" href="#schema-dumping" title="永久链接至标题">¶</a></h2>
<p>The <a class="reference internal" href="tools.html"><em>Phalcon Developer Tools</em></a> provides scripts to manage migrations (generation, running and rollback).</p>
<p>The available options for generating migrations are:</p>
<div class="figure align-center">
<img alt="../_images/migrations-1.png" src="../_images/migrations-1.png" />
</div>
<p>Running this script without any parameters will simply dump every object (tables and views) from your database in migration classes.</p>
<p>Each migration has a version identifier associated to it. The version number allows us to identify if the migration is newer or older than the current &#8216;version&#8217; of our database. Versions also inform Phalcon of the running order when executing a migration.</p>
<div class="figure align-center">
<img alt="../_images/migrations-2.png" src="../_images/migrations-2.png" />
</div>
<p>When a migration is generated, instructions are displayed on the console to describe the different steps of the migration and the execution time of those statements. At the end, a migration version is generated.</p>
<p>By default <a class="reference internal" href="tools.html"><em>Phalcon Developer Tools</em></a> use the <em>app/migrations</em> directory to dump the migration files. You can change the location by setting one of the parameters on the generation script. Each table in the database has its respective class generated in a separated file under a directory referring its version:</p>
<div class="figure align-center">
<img alt="../_images/migrations-3.png" src="../_images/migrations-3.png" />
</div>
</div>
<div class="section" id="migration-class-anatomy">
<h2>迁移类剖析（Migration Class Anatomy）<a class="headerlink" href="#migration-class-anatomy" title="永久链接至标题">¶</a></h2>
<p>Each file contains a unique class that extends the Phalcon\Mvc\Model\Migration These classes normally have two methods: up() and down(). Up() performs the migration, while down() rolls it back.</p>
<p>Up() also contains the <em>magic</em> method morphTable(). The magic comes when it recognizes the changes needed to synchronize the actual table in the database to the description given.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Db\Column</span> <span class="k">as</span> <span class="nx">Column</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Db\Index</span> <span class="k">as</span> <span class="nx">Index</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Db\Reference</span> <span class="k">as</span> <span class="nx">Reference</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">ProductsMigration_100</span> <span class="k">extends</span> <span class="nx">\Phalcon\Mvc\Model\Migration</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">up</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">morphTable</span><span class="p">(</span>
            <span class="s2">&quot;products&quot;</span><span class="p">,</span>
            <span class="k">array</span><span class="p">(</span>
                <span class="s2">&quot;columns&quot;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
                    <span class="k">new</span> <span class="nx">Column</span><span class="p">(</span>
                        <span class="s2">&quot;id&quot;</span><span class="p">,</span>
                        <span class="k">array</span><span class="p">(</span>
                            <span class="s2">&quot;type&quot;</span>          <span class="o">=&gt;</span> <span class="nx">Column</span><span class="o">::</span><span class="na">TYPE_INTEGER</span><span class="p">,</span>
                            <span class="s2">&quot;size&quot;</span>          <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">,</span>
                            <span class="s2">&quot;unsigned&quot;</span>      <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                            <span class="s2">&quot;notNull&quot;</span>       <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                            <span class="s2">&quot;autoIncrement&quot;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                            <span class="s2">&quot;first&quot;</span>         <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">),</span>
                    <span class="k">new</span> <span class="nx">Column</span><span class="p">(</span>
                        <span class="s2">&quot;product_types_id&quot;</span><span class="p">,</span>
                        <span class="k">array</span><span class="p">(</span>
                            <span class="s2">&quot;type&quot;</span>     <span class="o">=&gt;</span> <span class="nx">Column</span><span class="o">::</span><span class="na">TYPE_INTEGER</span><span class="p">,</span>
                            <span class="s2">&quot;size&quot;</span>     <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">,</span>
                            <span class="s2">&quot;unsigned&quot;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                            <span class="s2">&quot;notNull&quot;</span>  <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                            <span class="s2">&quot;after&quot;</span>    <span class="o">=&gt;</span> <span class="s2">&quot;id&quot;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">),</span>
                    <span class="k">new</span> <span class="nx">Column</span><span class="p">(</span>
                        <span class="s2">&quot;name&quot;</span><span class="p">,</span>
                        <span class="k">array</span><span class="p">(</span>
                            <span class="s2">&quot;type&quot;</span>    <span class="o">=&gt;</span> <span class="nx">Column</span><span class="o">::</span><span class="na">TYPE_VARCHAR</span><span class="p">,</span>
                            <span class="s2">&quot;size&quot;</span>    <span class="o">=&gt;</span> <span class="mi">70</span><span class="p">,</span>
                            <span class="s2">&quot;notNull&quot;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                            <span class="s2">&quot;after&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;product_types_id&quot;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">),</span>
                    <span class="k">new</span> <span class="nx">Column</span><span class="p">(</span>
                        <span class="s2">&quot;price&quot;</span><span class="p">,</span>
                        <span class="k">array</span><span class="p">(</span>
                            <span class="s2">&quot;type&quot;</span>    <span class="o">=&gt;</span> <span class="nx">Column</span><span class="o">::</span><span class="na">TYPE_DECIMAL</span><span class="p">,</span>
                            <span class="s2">&quot;size&quot;</span>    <span class="o">=&gt;</span> <span class="mi">16</span><span class="p">,</span>
                            <span class="s2">&quot;scale&quot;</span>   <span class="o">=&gt;</span> <span class="mi">2</span><span class="p">,</span>
                            <span class="s2">&quot;notNull&quot;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
                            <span class="s2">&quot;after&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;name&quot;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">),</span>
                <span class="p">),</span>
                <span class="s2">&quot;indexes&quot;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
                    <span class="k">new</span> <span class="nx">Index</span><span class="p">(</span>
                        <span class="s2">&quot;PRIMARY&quot;</span><span class="p">,</span>
                        <span class="k">array</span><span class="p">(</span><span class="s2">&quot;id&quot;</span><span class="p">)</span>
                    <span class="p">),</span>
                    <span class="k">new</span> <span class="nx">Index</span><span class="p">(</span>
                        <span class="s2">&quot;product_types_id&quot;</span><span class="p">,</span>
                        <span class="k">array</span><span class="p">(</span><span class="s2">&quot;product_types_id&quot;</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="p">),</span>
                <span class="s2">&quot;references&quot;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
                    <span class="k">new</span> <span class="nx">Reference</span><span class="p">(</span>
                        <span class="s2">&quot;products_ibfk_1&quot;</span><span class="p">,</span>
                        <span class="k">array</span><span class="p">(</span>
                            <span class="s2">&quot;referencedSchema&quot;</span>  <span class="o">=&gt;</span> <span class="s2">&quot;invo&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;referencedTable&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;product_types&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;columns&quot;</span>           <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s2">&quot;product_types_id&quot;</span><span class="p">),</span>
                            <span class="s2">&quot;referencedColumns&quot;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s2">&quot;id&quot;</span><span class="p">),</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
                <span class="p">),</span>
                <span class="s2">&quot;options&quot;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
                    <span class="s2">&quot;TABLE_TYPE&quot;</span>      <span class="o">=&gt;</span> <span class="s2">&quot;BASE TABLE&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;ENGINE&quot;</span>          <span class="o">=&gt;</span> <span class="s2">&quot;InnoDB&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;TABLE_COLLATION&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;utf8_general_ci&quot;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>The class is called &#8220;ProductsMigration_100&#8221;. Suffix 100 refers to the version 1.0.0. morphTable() receives an associative array with 4 possible sections:</p>
<table border="1" class="docutils">
<colgroup>
<col width="8%" />
<col width="85%" />
<col width="6%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Index</th>
<th class="head">Description</th>
<th class="head">Optional</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;columns&#8221;</td>
<td>An array with a set of table columns</td>
<td>No</td>
</tr>
<tr class="row-odd"><td>&#8220;indexes&#8221;</td>
<td>An array with a set of table indexes.</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td>&#8220;references&#8221;</td>
<td>An array with a set of table references (foreign keys).</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td>&#8220;options&#8221;</td>
<td>An array with a set of table creation options. These options are often related to the database system in which the migration was generated.</td>
<td>Yes</td>
</tr>
</tbody>
</table>
<div class="section" id="defining-columns">
<h3>定义列（Defining Columns）<a class="headerlink" href="#defining-columns" title="永久链接至标题">¶</a></h3>
<p><a class="reference internal" href="../api/Phalcon_Db_Column.html"><em>Phalcon\Db\Column</em></a> is used to define table columns. It encapsulates a wide variety of column related features. Its constructor receives as first parameter the column name and an array describing the column. The following options are available when describing columns:</p>
<table border="1" class="docutils">
<colgroup>
<col width="10%" />
<col width="84%" />
<col width="6%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Option</th>
<th class="head">Description</th>
<th class="head">Optional</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;type&#8221;</td>
<td>Column type. Must be a <a class="reference internal" href="../api/Phalcon_Db_Column.html"><em>Phalcon_Db_Column</em></a> constant (see below)</td>
<td>No</td>
</tr>
<tr class="row-odd"><td>&#8220;size&#8221;</td>
<td>Some type of columns like VARCHAR or INTEGER may have a specific size</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td>&#8220;scale&#8221;</td>
<td>DECIMAL or NUMBER columns may be have a scale to specify how much decimals it must store</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td>&#8220;unsigned&#8221;</td>
<td>INTEGER columns may be signed or unsigned. This option does not apply to other types of columns</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td>&#8220;notNull&#8221;</td>
<td>Column can store null values?</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td>&#8220;autoIncrement&#8221;</td>
<td>With this attribute column will filled automatically with an auto-increment integer. Only one column in the table can have this attribute.</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td>&#8220;first&#8221;</td>
<td>Column must be placed at first position in the column order</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td>&#8220;after&#8221;</td>
<td>Column must be placed after indicated column</td>
<td>Yes</td>
</tr>
</tbody>
</table>
<p>Database migrations support the following database column types:</p>
<ul class="simple">
<li>Phalcon\Db\Column::TYPE_INTEGER</li>
<li>Phalcon\Db\Column::TYPE_DATE</li>
<li>Phalcon\Db\Column::TYPE_VARCHAR</li>
<li>Phalcon\Db\Column::TYPE_DECIMAL</li>
<li>Phalcon\Db\Column::TYPE_DATETIME</li>
<li>Phalcon\Db\Column::TYPE_CHAR</li>
<li>Phalcon\Db\Column::TYPE_TEXT</li>
</ul>
</div>
<div class="section" id="defining-indexes">
<h3>定义索引（Defining Indexes）<a class="headerlink" href="#defining-indexes" title="永久链接至标题">¶</a></h3>
<p><a class="reference internal" href="../api/Phalcon_Db_Index.html"><em>Phalcon\Db\Index</em></a> defines table indexes. An index only requires that you define a name for it and a list of its columns. Note that if any index has the name PRIMARY, Phalcon will create a primary key index in that table.</p>
</div>
<div class="section" id="defining-references">
<h3>定义关系（Defining References）<a class="headerlink" href="#defining-references" title="永久链接至标题">¶</a></h3>
<p><a class="reference internal" href="../api/Phalcon_Db_Reference.html"><em>Phalcon\Db\Reference</em></a> defines table references (also called foreign keys). The following options can be used to define a reference:</p>
<table border="1" class="docutils">
<colgroup>
<col width="16%" />
<col width="77%" />
<col width="8%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Index</th>
<th class="head">Description</th>
<th class="head">Optional</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;referencedTable&#8221;</td>
<td>It&#8217;s auto-descriptive. It refers to the name of the referenced table.</td>
<td>No</td>
</tr>
<tr class="row-odd"><td>&#8220;columns&#8221;</td>
<td>An array with the name of the columns at the table that have the reference</td>
<td>No</td>
</tr>
<tr class="row-even"><td>&#8220;referencedColumns&#8221;</td>
<td>An array with the name of the columns at the referenced table</td>
<td>No</td>
</tr>
<tr class="row-odd"><td>&#8220;referencedTable&#8221;</td>
<td>The referenced table maybe is on another schema or database. This option allows you to define that.</td>
<td>Yes</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="writing-migrations">
<h2>创建迁移类（Writing Migrations）<a class="headerlink" href="#writing-migrations" title="永久链接至标题">¶</a></h2>
<p>Migrations aren&#8217;t only designed to &#8220;morph&#8221; table. A migration is just a regular PHP class so you&#8217;re not limited to these functions. For example after adding a column you could write code to set the value of that column for existing records. For more details and examples of individual methods, check the <a class="reference internal" href="db.html"><em>database component</em></a>.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">class</span> <span class="nc">ProductsMigration_100</span> <span class="k">extends</span> <span class="nx">\Phalcon\Mvc\Model\Migration</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">up</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//...</span>
        <span class="nx">self</span><span class="o">::</span><span class="nv">$_connection</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">(</span>
            <span class="s2">&quot;products&quot;</span><span class="p">,</span>
            <span class="k">array</span><span class="p">(</span><span class="s2">&quot;Malabar spinach&quot;</span><span class="p">,</span> <span class="mf">14.50</span><span class="p">),</span>
            <span class="k">array</span><span class="p">(</span><span class="s2">&quot;name&quot;</span><span class="p">,</span> <span class="s2">&quot;price&quot;</span><span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="running-migrations">
<h2>执行迁移（Running Migrations）<a class="headerlink" href="#running-migrations" title="永久链接至标题">¶</a></h2>
<p>Once the generated migrations are uploaded on the target server, you can easily run them as shown in the following example:</p>
<div class="figure align-center">
<img alt="../_images/migrations-4.png" src="../_images/migrations-4.png" />
</div>
<div class="figure align-center">
<img alt="../_images/migrations-5.png" src="../_images/migrations-5.png" />
</div>
<p>Depending on how outdated is the database with respect to migrations, Phalcon may run multiple migration versions in the same migration process. If you specify a target version, Phalcon will run the required migrations until it reaches the specified version.</p>
</div>
</div>


                    </div>
                  </div>
                </div>
            </div>
          </td>
        </tr>
      </table>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="debug.html" title="调试应用程序（Debugging Applications）"
             >下一页</a> |</li>
        <li class="right" >
          <a href="intl.html" title="国际化（Internationalization）"
             >上一页</a> |</li> 
      </ul>
    </div>

      <div class="prefooter">
  <div class="container">
      <div class="row">
          <div class="col-sm-3 text-right">
              <span>Follow along:</span>
          </div>
          <div class="col-sm-6 text-center">
              <a href="https://twitter.com/phalconphp" alt="Twitter" class="btn-social btn-social-twitter"><i class="icon-twitter"></i></a>
              <a href="https://www.facebook.com/pages/Phalcon-Framework/134230726685897" alt="Facebook" class="btn-social btn-social-facebook"><i class="icon-facebook"></i></a>
              <a href="https://plus.google.com/102376109340560896457" alt="Google+" class="btn-social btn-social-googleplus"><i class="icon-googleplus"></i></a>
              <a href="https://github.com/phalcon/cphalcon" alt="Github" class="btn-social btn-social-github"><i class="icon-github"></i></a>
          </div>
          <div class="col-sm-3">
          </div>
      </div>
  </div>

</div>
<footer class="footer">
  <div class="container">
      <div class="row">
          <div class="col-xs-4 col-sm-3">
              <h4>Download</h4>
              <ul>
                  <li><a href="http://phalconphp.com/download">Installing Phalcon</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://api.phalconphp.com">API</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html">Tutorial</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html#sample-applications">Sample Applications</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-3">
              <h4>Community</h4>
              <ul>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon">GitHub</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon/issues">Issue Tracker</a></li>
                  <li><a href="http://stackoverflow.com/questions/tagged/phalcon">Stack Overflow</a></li>
                  <li><a href="http://phalconphp.com/en/testimonials">Testimonials</a></li>
                  <li><a href="http://builtwith.phalconphp.com/">Built with Phalcon</a></li>
                  <li><a href="http://store.phalconphp.com/">Store</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-2">
              <h4>About</h4>
              <ul>
                  <li><a class="link-black" href="http://blog.phalconphp.com/">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                  <li><a href="http://phalconphp.com/en/team">Team</a></li>
                  <li><a href="http://phalconphp.com/en/roadmap">Roadmap</a></li>
                  <li><a href="http://phalconphp.com/en/donate">Donate</a></li>
                  <li><a href="http://phalconphp.com/en/consulting">Consulting</a></li>
                  <li><a href="http://phalconphp.com/en/hosting">Hosting</a></li>
              </ul>
          </div>
          <div id="license-spaccer" class="visible-xs"></div>
          <div id="license-wrapper" class="col-xs-12 col-sm-4">
              <p class="license">

                  Found a typo or an error? Want to improve this document? The documentation sources are available on <a href="http://github.com/phalcon/docs">Github</a><br>
                  Need support or have questions? Check our <a href="http://forum.phalconphp.com">Forum</a><br>
                  <br>

                  Phalcon Framework is released under the <a href="https://github.com/phalcon/cphalcon/blob/master/docs/LICENSE.md">new BSD license</a>.<br>
                  Except where otherwise noted, content on this site is licensed under the
                    <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License.</a>

                <div class="design">
                  <span>Designed by:</span>

                  <a href="http://www.fog-city.net/" class="fogcity" target="_blank" title="Fog City Software"><span>Fog City Software</span></a>
              </div>
          </div>
      </div>
  </div>
</footer>

    </div>
    <script type="text/javascript">
    $(window).on("load", function(){
      var cx = '009733439235723428699:lh9ltjgvdz8';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    });
    </script>

  </body>
</html>